package net.xdclass.service.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ServiceController {

    @Value("${user.aaa}")
    private String userName;

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/service")
    public List<String> getServiceList(){
        return discoveryClient.getServices();
    }

    @GetMapping("/instance")
    public Object getInstance(@RequestParam("name") String name){
        return discoveryClient.getInstances(name);
    }

    /**
     * 阿里ACM测试用接口
     *
     * @return
     */
    @RequestMapping("/testAcm")
    public String testAcm() {
        return userName;
    }

    @GetMapping("/serviceAndPods")
    public Map getServiceAndPodList(){
        List<String> services = discoveryClient.getServices();
        Map map = new HashMap();
        for(String s : services){
            map.put(s, discoveryClient.getInstances(s));
        }
        return map;
    }
}
